package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 **/
@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {

    @Resource
    private PayService payService;


    @Operation(summary = "新增",description ="新增支付流水方法,json串做参数" )
    @PostMapping("pay/add")
    public ResultData addPay(@RequestBody Pay pay){ //json接收
        log.info("新增pay",pay);
        int i = payService.add(pay);
        return ResultData.success(null);
    }

    @Operation(summary = "删除",description = "删除支付流水方法")
    @DeleteMapping("pay/del/{id}")
    public ResultData deleteById(@PathVariable("id") Integer id){
        int i = payService.delete(id);
        return ResultData.success(null);
    }


    @Operation(summary = "修改",description = "修改支付流水方法")
    @PutMapping("pay/update")
    public ResultData updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO,pay);
        payService.update(pay);
        return ResultData.success("成功更新数据！");

    }
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    @GetMapping("pay/get/{id}")
    public ResultData getById(@PathVariable("id") Integer id){
        if (id==-1){
            throw new RuntimeException("id不能为负数");
        }
        Pay pay = payService.getById(id);
        return ResultData.success(pay);

    }


    //全部查询
    @Operation(summary = "查询全部流水",description = "返回一个list")
    @GetMapping("/pay/getAll")
    public ResultData getAll(){
        List<Pay> all = payService.getAll();
        return ResultData.success(all);
    }


    @Value("${server.port}")
    private String port;

    @GetMapping("/pay/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo){
        return "atguiguInfo: "+atguiguInfo+"---" +"port"+port;

    }

}
